﻿
namespace NetROS.Tiers.Windows.Forms
{
	/// <summary>
	/// abstract RabatSpec typed datagridview
	/// </summary>
	[System.ComponentModel.DesignerCategoryAttribute("code")]
	public abstract class RabatSpecDataGridViewBase : System.Windows.Forms.UserControl
	{	
		/// <summary> 
		/// Thrown when the current Entity changed.
		/// </summary>
		public event System.EventHandler<RabatSpecDataGridViewEventArgs> CurrentEntityChanged;
		
		#region Fields
				
		/// <summary> 
		/// the current entity.
		/// </summary>
		private Entities.RabatSpec _currentRabatSpec = null;
		//private Dictionary<string, string> _columnsHeaders = new Dictionary<string, string>();
		
		/// <summary> 
		/// the datagridview instance
		/// </summary>
		protected System.Windows.Forms.DataGridView uxRabatSpecDataGridView;
		
		/// <summary> 
		/// the errorprovider
		/// </summary>
		protected System.Windows.Forms.ErrorProvider uxRabatSpecErrorProvider;
		
		/// <summary> 
		/// the bindingsource
		/// </summary>
		protected System.Windows.Forms.BindingSource uxRabatSpecBindingSource;
		
		
		/// <summary> 
		/// the DGV column associated with the ID property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxIDDataGridViewColumn;
		/// <summary> 
		/// the DGV column associated with the IDRabat property
		/// </summary>
		protected System.Windows.Forms.DataGridViewComboBoxColumn uxIDRabatDataGridViewColumn;
		/// <summary> 
		/// the DGV column associated with the IDSpec property
		/// </summary>
		protected System.Windows.Forms.DataGridViewComboBoxColumn uxIDSpecDataGridViewColumn;
		
		/// <summary> 
		/// the DGV column associated with the Vrijed property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxVrijedDataGridViewColumn;
		
		#endregion
					
		#region Combo list
		
				
		private Entities.TList<Entities.ArtiklSpecifikacija> _IDSpecList;
		
		/// <summary> 
		/// The list of selectable ArtiklSpecifikacija
		/// </summary>
		[System.ComponentModel.Category("DropDownList BindingSources")]
		public Entities.TList<Entities.ArtiklSpecifikacija> IDSpecList
		{
			get {return this._IDSpecList;}
			set 
			{
				this._IDSpecList = value;
				this.uxIDSpecDataGridViewColumn.DataSource = null;
				this.uxIDSpecDataGridViewColumn.DataSource = this._IDSpecList;
			}
		}
		
		private bool _allowNewItemInIDSpecList;
		
		/// <summary> 
		/// Indicates if user can add an item in the list of ArtiklSpecifikacija
		/// </summary>
		[System.ComponentModel.Category(".netTiers"), System.ComponentModel.Description("Indicates if end user can add a new item in the combobox. an event will be thrown then.")]
		public bool AllowNewItemInIDSpecList
		{
			get { return _allowNewItemInIDSpecList;}
			set
			{
				this._allowNewItemInIDSpecList = value;
				this.uxIDSpecDataGridViewColumn.DisplayStyle = value ? System.Windows.Forms.DataGridViewComboBoxDisplayStyle.ComboBox : System.Windows.Forms.DataGridViewComboBoxDisplayStyle.DropDownButton;
			}
		}
		
				
		private Entities.TList<Entities.Rabat> _IDRabatList;
		
		/// <summary> 
		/// The list of selectable Rabat
		/// </summary>
		[System.ComponentModel.Category("DropDownList BindingSources")]
		public Entities.TList<Entities.Rabat> IDRabatList
		{
			get {return this._IDRabatList;}
			set 
			{
				this._IDRabatList = value;
				this.uxIDRabatDataGridViewColumn.DataSource = null;
				this.uxIDRabatDataGridViewColumn.DataSource = this._IDRabatList;
			}
		}
		
		private bool _allowNewItemInIDRabatList;
		
		/// <summary> 
		/// Indicates if user can add an item in the list of Rabat
		/// </summary>
		[System.ComponentModel.Category(".netTiers"), System.ComponentModel.Description("Indicates if end user can add a new item in the combobox. an event will be thrown then.")]
		public bool AllowNewItemInIDRabatList
		{
			get { return _allowNewItemInIDRabatList;}
			set
			{
				this._allowNewItemInIDRabatList = value;
				this.uxIDRabatDataGridViewColumn.DisplayStyle = value ? System.Windows.Forms.DataGridViewComboBoxDisplayStyle.ComboBox : System.Windows.Forms.DataGridViewComboBoxDisplayStyle.DropDownButton;
			}
		}
		
		#endregion
		
		#region Main Datasource
		
		private Entities.TList<Entities.RabatSpec> _RabatSpecList;
				
		/// <summary> 
		/// The list of RabatSpec to be displayed in the DGV.
		/// </summary>
		[System.ComponentModel.Category("Data")]
		public Entities.TList<Entities.RabatSpec> RabatSpecList
		{
			get {return this._RabatSpecList;}
			set
			{
				this._RabatSpecList = value;
				this.uxRabatSpecBindingSource.DataSource = null;
				this.uxRabatSpecBindingSource.DataSource = value;
				this.uxRabatSpecDataGridView.DataSource = null;
				this.uxRabatSpecDataGridView.DataSource = this.uxRabatSpecBindingSource;				
				//this.uxRabatSpecBindingSource.ListChanged += new System.ComponentModel.ListChangedEventHandler(uxRabatSpecBindingSource_ListChanged);
				this.uxRabatSpecBindingSource.CurrentItemChanged += new System.EventHandler(OnRabatSpecBindingSourceCurrentItemChanged);
			}
		}
		
		private void OnRabatSpecBindingSourceCurrentItemChanged(object sender, System.EventArgs e)
		{
			_currentRabatSpec = uxRabatSpecBindingSource.Current as Entities.RabatSpec;
			
			if (_currentRabatSpec != null)
			{
				_currentRabatSpec.Validate();
			}
			//_RabatSpec.Validate();
			OnCurrentEntityChanged();
		}

		//void uxRabatSpecBindingSource_ListChanged(object sender, System.ComponentModel.ListChangedEventArgs e)
		//{
			//throw new Exception("The method or operation is not implemented.");
		//}		
		
		#endregion
		
		/// <summary>
		/// Gets the currently selected <c cref="Entities.RabatSpec"/> instance.
		/// </summary>
		public Entities.RabatSpec SelectedRabatSpec
		{
			get {return this._currentRabatSpec;}
		}
		
		#region other properties
		
		/// <summary>
		/// Gets or sets a value indicating whether you have provided your own data-management operations for the DataGridView control.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool VirtualMode
		{
			get {return this.uxRabatSpecDataGridView.VirtualMode;}
			set
			{
				this.uxRabatSpecDataGridView.VirtualMode = value;
			}
		}
		
		/// <summary>
		/// Gets or sets a value indicating whether the option to add rows is displayed to the user.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool AllowUserToAddRows
		{
			get {return this.uxRabatSpecDataGridView.AllowUserToAddRows;}
			set {this.uxRabatSpecDataGridView.AllowUserToAddRows = value;}
		}
		
		/// <summary>
		/// Gets or sets a value indicating whether the user is allowed to delete rows from the DataGridView.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool AllowUserToDeleteRows
		{
			get {return this.uxRabatSpecDataGridView.AllowUserToDeleteRows;}
			set {this.uxRabatSpecDataGridView.AllowUserToDeleteRows = value;}
		}
		
		/// <summary>
		/// Gets the <c cref="DataGridView"/> instance.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public System.Windows.Forms.DataGridView DataGridView
		{
			get {return this.uxRabatSpecDataGridView;}
		}
		
		/// <summary>
		/// Gets a collection that contains all the columns in the control.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]		
		[System.ComponentModel.DesignerSerializationVisibilityAttribute(System.ComponentModel.DesignerSerializationVisibility.Visible)]
		//[System.ComponentModel.EditorAttribute("System.Windows.Forms.Design.DataGridViewColumnCollectionEditor, System.Design", typeof(System.Drawing.Design.UITypeEditor))] 
		public System.Windows.Forms.DataGridViewColumnCollection Columns
		{
			get { return this.uxRabatSpecDataGridView.Columns; }
		}
		
		#endregion
		
		/// <summary> 
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.IContainer components = null;
	
		/// <summary> 
		/// Clean up any resources being used.
		/// </summary>
		/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
		protected override void Dispose(bool disposing)
		{
			if (disposing && (components != null))
			{
				components.Dispose();
			}
			base.Dispose(disposing);
		}
	
		#region Constructor
	
		/// <summary>
		/// Initializes a new instance of the <see cref="RabatSpecDataGridViewBase"/> class.
		/// </summary>
		public RabatSpecDataGridViewBase()
		{			
			this.components = new System.ComponentModel.Container();			
			
			this.uxRabatSpecDataGridView = new System.Windows.Forms.DataGridView();
			this.uxRabatSpecBindingSource = new System.Windows.Forms.BindingSource(this.components);
			this.uxRabatSpecErrorProvider = new System.Windows.Forms.ErrorProvider(this.components);
			this.uxIDDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.uxIDRabatDataGridViewColumn = new System.Windows.Forms.DataGridViewComboBoxColumn();
			this.uxIDSpecDataGridViewColumn = new System.Windows.Forms.DataGridViewComboBoxColumn();
			this.uxVrijedDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			//this.uxIDSpecBindingSource = new ArtiklSpecifikacijaBindingSource(this.components);
			//((System.ComponentModel.ISupportInitialize)(this.uxIDSpecBindingSource)).BeginInit();
			//this.uxIDRabatBindingSource = new RabatBindingSource(this.components);
			//((System.ComponentModel.ISupportInitialize)(this.uxIDRabatBindingSource)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.uxRabatSpecDataGridView)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.uxRabatSpecBindingSource)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.uxRabatSpecErrorProvider)).BeginInit();
			this.SuspendLayout();
						
			// 
			// uxRabatSpecErrorProvider
			// 
			this.uxRabatSpecErrorProvider.ContainerControl = this;
			this.uxRabatSpecErrorProvider.DataSource = this.uxRabatSpecBindingSource;						
			// 
			// uxRabatSpecDataGridView
			// 
			this.uxRabatSpecDataGridView.AutoGenerateColumns = false;
			this.uxRabatSpecDataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
			this.uxRabatSpecDataGridView.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
		this.uxIDDataGridViewColumn,
		this.uxIDRabatDataGridViewColumn,
		this.uxIDSpecDataGridViewColumn,
		this.uxVrijedDataGridViewColumn			});
			this.uxRabatSpecDataGridView.Dock = System.Windows.Forms.DockStyle.Fill;
			this.uxRabatSpecDataGridView.Location = new System.Drawing.Point(0, 0);
			this.uxRabatSpecDataGridView.Name = "uxRabatSpecDataGridView";
			this.uxRabatSpecDataGridView.Size = new System.Drawing.Size(470, 300);
			this.uxRabatSpecDataGridView.TabIndex = 0;	
			this.uxRabatSpecDataGridView.BackgroundColor = System.Drawing.Color.WhiteSmoke;
			this.uxRabatSpecDataGridView.EnableHeadersVisualStyles = false;
			this.uxRabatSpecDataGridView.DataError += new System.Windows.Forms.DataGridViewDataErrorEventHandler(this.OnRabatSpecDataGridViewDataError);
			this.uxRabatSpecDataGridView.CellValueNeeded += new System.Windows.Forms.DataGridViewCellValueEventHandler(this.OnRabatSpecDataGridViewCellValueNeeded);
			this.uxRabatSpecDataGridView.CellValuePushed += new System.Windows.Forms.DataGridViewCellValueEventHandler(this.OnRabatSpecDataGridViewCellValuePushed);
			
			//
			// uxIDDataGridViewColumn
			//
			this.uxIDDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxIDDataGridViewColumn.DataPropertyName = "ID";
			this.uxIDDataGridViewColumn.HeaderText = "ID";
			this.uxIDDataGridViewColumn.Name = "uxIDDataGridViewColumn";
			this.uxIDDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxIDDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxIDDataGridViewColumn.ReadOnly = true;		
			//
			// uxIDRabatDataGridViewColumn
			//
			this.uxIDRabatDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxIDRabatDataGridViewColumn.DataPropertyName = "IDRabat";
			this.uxIDRabatDataGridViewColumn.HeaderText = "IDRabat";
			this.uxIDRabatDataGridViewColumn.Name = "uxIDRabatDataGridViewColumn";
			this.uxIDRabatDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxIDRabatDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxIDRabatDataGridViewColumn.ReadOnly = false;		
			//
			// uxIDSpecDataGridViewColumn
			//
			this.uxIDSpecDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxIDSpecDataGridViewColumn.DataPropertyName = "IDSpec";
			this.uxIDSpecDataGridViewColumn.HeaderText = "IDSpec";
			this.uxIDSpecDataGridViewColumn.Name = "uxIDSpecDataGridViewColumn";
			this.uxIDSpecDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxIDSpecDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxIDSpecDataGridViewColumn.ReadOnly = false;		
			//
			// uxVrijedDataGridViewColumn
			//
			this.uxVrijedDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxVrijedDataGridViewColumn.DataPropertyName = "Vrijed";
			this.uxVrijedDataGridViewColumn.HeaderText = "Vrijed";
			this.uxVrijedDataGridViewColumn.Name = "uxVrijedDataGridViewColumn";
			this.uxVrijedDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxVrijedDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxVrijedDataGridViewColumn.ReadOnly = false;		
			//
			// uxIDSpecDataGridViewColumn
			//				
			this.uxIDSpecDataGridViewColumn.DisplayMember = "Naziv";	
			this.uxIDSpecDataGridViewColumn.ValueMember = "ID";	
			this.uxIDSpecDataGridViewColumn.DisplayStyleForCurrentCellOnly = true;
			//uxIDSpecDataGridViewColumn.DataSource = uxIDSpecBindingSource;				
				
			//
			// uxIDRabatDataGridViewColumn
			//				
			this.uxIDRabatDataGridViewColumn.DisplayMember = "IDKlijent";	
			this.uxIDRabatDataGridViewColumn.ValueMember = "ID";	
			this.uxIDRabatDataGridViewColumn.DisplayStyleForCurrentCellOnly = true;
			//uxIDRabatDataGridViewColumn.DataSource = uxIDRabatBindingSource;				
				
			
			// this
			this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
			this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
			this.Controls.Add(this.uxRabatSpecDataGridView);
			this.Name = "RabatSpecDataGridView";
			this.Size = new System.Drawing.Size(470, 300);
			//((System.ComponentModel.ISupportInitialize)(this.uxIDSpecBindingSource)).EndInit();
			//((System.ComponentModel.ISupportInitialize)(this.uxIDRabatBindingSource)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.uxRabatSpecErrorProvider)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.uxRabatSpecDataGridView)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.uxRabatSpecBindingSource)).EndInit();
			this.ResumeLayout(false);
		}
		#endregion
				
		#region events
		
		/// <summary>
		/// Raised the CurrentEntityChanged event.
		/// </summary>
		protected void OnCurrentEntityChanged()
		{
			if (CurrentEntityChanged != null)
			{
				RabatSpecDataGridViewEventArgs args = new RabatSpecDataGridViewEventArgs();
				args.RabatSpec = _currentRabatSpec;
				CurrentEntityChanged(this, args); 
			}
		}
		
		#endregion
		
		/// <summary>
		/// Strongly typed event 
		/// </summary>
		public class RabatSpecDataGridViewEventArgs : System.EventArgs
		{
			private Entities.RabatSpec	_RabatSpec;
	
			/// <summary>
			/// the  Entities.RabatSpec instance.
			/// </summary>
			public Entities.RabatSpec RabatSpec
			{
				get { return _RabatSpec; }
				set { _RabatSpec = value; }
			}	
		}
		
		/// <summary>
		/// Handles the DataError event of the uxRabatSpecDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewDataErrorEventArgs"/> instance containing the event data.</param>
		protected void OnRabatSpecDataGridViewDataError(object sender, System.Windows.Forms.DataGridViewDataErrorEventArgs e)
		{
			System.Diagnostics.Debug.WriteLine(string.Format( "{0}, {1}", e.ColumnIndex, e.RowIndex));
		}
		
		/// <summary>
		/// Handles the CellValueNeeded event of the uxRabatSpecDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewCellValueEventArgs"/> instance containing the event data.</param>
		protected void OnRabatSpecDataGridViewCellValueNeeded(object sender, System.Windows.Forms.DataGridViewCellValueEventArgs e)
		{
			switch(uxRabatSpecDataGridView.Columns[e.ColumnIndex].Name)
			{
					case "uxIDDataGridViewColumn":
						e.Value = RabatSpecList[e.RowIndex].ID;
						break;
					case "uxIDRabatDataGridViewColumn":
						e.Value = RabatSpecList[e.RowIndex].IDRabat;
						break;
					case "uxIDSpecDataGridViewColumn":
						e.Value = RabatSpecList[e.RowIndex].IDSpec;
						break;
					case "uxVrijedDataGridViewColumn":
						e.Value = RabatSpecList[e.RowIndex].Vrijed;
						break;
				default:
				break;
			}
		}
		
		/// <summary>
		/// Handles the CellValuePushed event of the uxRabatSpecDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewCellValueEventArgs"/> instance containing the event data.</param>
		protected void OnRabatSpecDataGridViewCellValuePushed(object sender, System.Windows.Forms.DataGridViewCellValueEventArgs e)
		{
			switch(uxRabatSpecDataGridView.Columns[e.ColumnIndex].Name)
			{
					case "uxIDDataGridViewColumn":
						RabatSpecList[e.RowIndex].ID = (System.Int32)e.Value;
						break;
					case "uxIDRabatDataGridViewColumn":
						RabatSpecList[e.RowIndex].IDRabat = (System.Int32)e.Value;
						break;
					case "uxIDSpecDataGridViewColumn":
						RabatSpecList[e.RowIndex].IDSpec = (System.Int32)e.Value;
						break;
					case "uxVrijedDataGridViewColumn":
						RabatSpecList[e.RowIndex].Vrijed = (System.String)e.Value;
						break;
				default:
				break;
			}
		}
	}
}
